package com.alibaba.crs.service;
import com.alibaba.crs.entity.Bus;
import com.alibaba.crs.entity.Car;
import com.alibaba.crs.entity.MotorVehicle;
import com.alibaba.crs.entity.Truck;
/**
 * @author GuoBin Hao
 * @version v1.0
 * @createDate 2021-3-31
 * 汽车业务类
 */
public class MotorVehicleService {
    /**
     * 声明保存车辆数据集合
     */
    private static MotorVehicle[] vehicles = new MotorVehicle[20];

    /**
     * 初始化车辆数据
     */
    static {
        vehicles[0] = new Car("京NY28588","宝马","X6",800);
        vehicles[1] = new Car("京CNY3284","宝马","550i",600);
        vehicles[2] = new Car("京NT37465","别克","林荫大道",300);
        vehicles[3] = new Car("京NT96968","别克","GL8",600);
        vehicles[4] = new Bus("京6566754","金杯",800,16);
        vehicles[5] = new Bus("京8696997","金龙",800,16);
        vehicles[6] = new Bus("京9696996","金杯",1500,34);
        vehicles[7] = new Bus("京8696998","金龙",1500,34);
        vehicles[8] = new Truck("京MH98725","一汽解放",2);
        vehicles[9] = new Truck("京HN56731","重庆红岩",2);
    }

    /**
     * 实现租车的方法
     * @param brand 品牌
     * @param carType 型号
     * @param seatNum 座位数
     * @param load 载重量
     * @return 返回汽车对象
     */
    public static MotorVehicle rentVehicle(
            String brand,
            String carType,
            int seatNum,
            int load){
        MotorVehicle v = null;
        for (int i = 0,len = vehicles.length; i < len; i++) {
            v = vehicles[i];
            if(v == null)
                continue;
            if(v instanceof Car){
                Car car = (Car)v;
                if(car.getBrand().equals(brand)
                        && car.getCarType().equals(carType)){
                    v = car;
                    break;
                }
            }
            else if(v instanceof Bus){
                Bus bus = (Bus)v;
                if(bus.getBrand().equals(brand) && bus.getSeatNum() == seatNum){
                    v = bus;
                    break;
                }
            }
            else if(v instanceof Truck){
                Truck truck = (Truck)v;
                if(truck.getBrand().equals(brand) && truck.getLoad() == load){
                    v = truck;
                    break;
                }
            }
        }
        return v;
    }
}
